package ar.uba.fi.tonyvaliente.commands;

import ar.uba.fi.tonyvaliente.documents.Document;
import ar.uba.fi.tonyvaliente.documents.DocumentManager;

public class ShowDocumentCommand extends Command {

	private boolean recognized;
	private int documentId;

	public ShowDocumentCommand(String[] args) {
		super(args);

		recognized = false;
		documentId = 0;

		if (args.length > 0) {
			String command = args[0];
			command = command.toLowerCase();
			if (command.equals("-showdocument")) {
				recognized = true;
				if (args.length > 1) {
					documentId = Integer.parseInt(args[1]);
				}
			}
		}
	}

	private CommandResult showDocument(int documentId) {
		Document document = DocumentManager.getDocument(documentId);
		if (document != null)
			return new CommandResult(true, true, document.getContent());
		else
			return new CommandResult(true, false, "Invalid document id");
	}

	public CommandResult execute() {
		if (!recognized)
			return CommandResult.NOT_RECOGNIZED;

		if (documentId <= 0)
			return new CommandResult(true, false, "Missing document id");

		return showDocument(documentId);
	}

	public String getSignature() {
		return "-showDocument \"documentId\"";
	}

	public String getDescription() {
		return "Show specified document";
	}

	public String[] getExamples() {
		return new String[] { String.format("-showDocument \"%s\"", "133") };
	}
}
